
(function () { 
	Lhs.ui.CreateSearchWindow = function () {
	var pathimage=Ti.App.Properties.getString('pathimage');
    var win = Titanium.UI.createWindow({
        id: 'search',
        title: 'TÌM KIẾM',
        fullscreen: true,
        navBarHidden:true,
        backgroundImage:pathimage+'bg.png',
    });
    
    var egc = new globals.EventGarbageCollection(win);
    egc.addEventListener('back_'+win.id, function () {
    	Lhs.navGroup.close(win, {
             animated: true
        });
    	
	});
	
	//HEADER ON WINDOWS
    var main_header=require('controls/header').Createheader({
		  	idWinParent:win.id,
		  	height:Ti.App.Properties.getInt('height_Header'),//44,
		  	title:'TÌM KIẾM',
    })
	win.add(main_header);
	//Search bar
	/*
	var vi_search = Titanium.UI.createView({
		top:main_header.height,
		height:40,
	    left:0,
	    width:Titanium.Platform.displayCaps.platformWidth,
		backgroundImage: pathimage+'Search.png',
	}); 
	win.add(vi_search)
	*/
	//alert(main_header.height)
	var search_string = Titanium.UI.createTextField({
	    borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
	    top:main_header.height,
	    height:  Ti.App.Properties.getInt('height_Search'),//'auto',
	    left:0,
	    width:Titanium.Platform.displayCaps.platformWidth,
	    hintText:'Nhập từ khóa tìm kiếm',
	    keyboardType: Titanium.UI.KEYBOARD_DEFAULT,
	    returnKeyType: Titanium.UI.RETURNKEY_SEARCH , 
	   
	});
	//alert(search_string.getheight)
	search_string.addEventListener('return', function(e)
	{
	
		search_string.blur();
	
		if (Ti.Network.online) {
		 	 SearchOnline(e.source.value)
		}
		else
		{
			
		 	 SearchOffline(e.source.value)
		}
	  
	  
	});
	win.add(search_string);
	
	//TABLE VIEW
    var table_Category=Ti.UI.createTableView({
	  	backgroundColor:'Transparent',
	  	separatorColor:'Transparent',
	  	top:search_string.height+search_string.top+3,
    })
    table_Category.addEventListener('click', function (e) {
		Lhs.navGroup.open(Lhs.ui.CreateCompanyDetailWindow({
			cid:e.rowData.id,
		}), 
		{
		    animated: true,
		    navBarHidden:true
		});	
	});
	win.add(table_Category);
	
	///////////////////LOADING WINDOWS
	 var LoadListData = function(e) {
    	var datajson=e.datajson;
 		json = JSON.parse(datajson);
		return json;
    }
	 var LoadListDataLocal = function(e) {
 		var json=require('db').selectCategoryDetailAll();
		return json;
     }
     var CreateTableViewRow = function(e) {
     	var item=e.data;
        var row = Ti.UI.createTableViewRow({
            height:Ti.App.Properties.getInt('height_TableviewRow'),//90,
			id:item.id,
			name:item.title
		})
		var Image_Left = Ti.UI.createImageView({
				image:item.logo,
				left:0,
				width:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
				height:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
		})
		
		
		
		var nameLabel = Ti.UI.createLabel({
            text:item.title,
            font:{
                fontSize:Ti.App.Properties.getInt('fontsize_TableviewRow'),//'17dp',
            	fontWeight:'bold'
            },
            left:Ti.App.Properties.getInt('height_TableviewRow'),//80,
            color:'#724b37',
        });
        var vi_line = Titanium.UI.createView({
			bottom:0,
			width:'100%',
			height:2,
			backgroundImage: pathimage+'img_devide_line_setting.png',
		 	touchEnabled: false,
		}); 
		
		row.add(nameLabel);
		row.add(Image_Left);
		row.add(vi_line)
		return row;
     }
     var LoadList = function(e) {
     	/*
		var datajson=e.datajson;
		//Ti.API.info (datajson)
 		var tableData=[];
 		//Convert Text to Json Array
 		json = JSON.parse(datajson);
 		//Ti.API.info (json.length)
 		//alert(json.length)
 		*/
 		
 		
 	
 		
 		
 		var tableData=[];
 		var json=[];
 		
 		if(e.isconnect==1)
 		{
 			json=LoadListData(e);
 		}
 		else
 		{
 			json=LoadListDataLocal(e);
 		}
 		
 		
 		/*
	    //Import data Json to Tableview
	    
	    for (i = 0; i < json.length; i++) {
	        var item = json[i];
			
	        //if(e.isconnect==1)
	 		//{
	 		//	//Insert data when connect
	        //	require('db').insertCategoryDetail(item,arg.id)
	 		//}
	 		
			Ti.API.info (item)
	        var row = Ti.UI.createTableViewRow({
	            height:Ti.App.Properties.getInt('height_TableviewRow'),//90,
				id:item.id,
				name:item.title
			})
			var Image_Left = Ti.UI.createImageView({
					image:item.logo,
					left:0,
					width:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
					height:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
			})
			
			
			
			var nameLabel = Ti.UI.createLabel({
	            text:item.title,
	            font:{
	                fontSize:Ti.App.Properties.getInt('fontsize_TableviewRow'),//'17dp',
	            	fontWeight:'bold'
	            },
	            left:Ti.App.Properties.getInt('height_TableviewRow'),//80,
	            color:'#724b37',
	        });
	        var vi_line = Titanium.UI.createView({
				bottom:0,
				width:'100%',
				height:2,
				backgroundImage: pathimage+'img_devide_line_setting.png',
			 	touchEnabled: false,
			}); 
			
			row.add(nameLabel);
			row.add(Image_Left);
			row.add(vi_line)
	       // tableData.push(row);
			 table_Category.appendRow(row)
        }
 		//table_Category.setData(tableData);
 		//Lhs.ui.activityIndicator.hideModal();
 		
 		*/
 		require('settings').LoadingData2TableView(table_Category,json,CreateTableViewRow,15);
 		search_string.blur();
	};
	
	var LoadList_error = function(e) {
		
		
		if(Ti.Platform.osname == 'android')
		{
			require('settings').createAlertDialog(Globals.title,Globals.connecterror);
			//Lhs.ui.activityIndicator.hideModal();
		}
		else
	   		require('settings').Alert_error(win);
	};
	 
	
	 
	var SearchOnline = function(keyword) {
		table_Category.setData([]);
		var jsonurl='http://www.e-media.vn/emedia.php?keyword='+keyword;
		var param='';
		//Lhs.ui.activityIndicator.showModal(Globals.search_pop, Globals.timeout, Globals.loadingTimeout);
		require('settings').GetJsonFunction_GET(jsonurl,param,LoadList,LoadList_error);
	};
	
	 
	var SearchOffline = function(keyword) {
		table_Category.setData([]);
		LoadListSearchOffline(keyword)
	};
	
	 var LoadListSearchOffline = function(keyword) {
	 
	 	
     
 		var tableData=[];
 		var json=[];
 		json=require('db').SearchCategoryDetailAll(keyword);
 		
	    //Import data Json to Tableview
	    /*
	    for (i = 0; i < json.length; i++) {
	        var item = json[i];
			Ti.API.info (item)
	        var row = Ti.UI.createTableViewRow({
	            height:Ti.App.Properties.getInt('height_TableviewRow'),//90,
				id:item.id,
				name:item.title
			})
			var Image_Left = Ti.UI.createImageView({
					image:item.logo,
					left:0,
					width:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
					height:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
			})
			
			
			
			var nameLabel = Ti.UI.createLabel({
	            text:item.title,
	            font:{
	                fontSize:Ti.App.Properties.getInt('fontsize_TableviewRow'),//'17dp',
	            	fontWeight:'bold'
	            },
	            left:Ti.App.Properties.getInt('height_TableviewRow'),//80,
	            color:'#724b37',
	        });
	        var vi_line = Titanium.UI.createView({
				bottom:0,
				width:'100%',
				height:2,
				backgroundImage: pathimage+'img_devide_line_setting.png',
			 	touchEnabled: false,
			}); 
			
			row.add(nameLabel);
			row.add(Image_Left);
			row.add(vi_line)
	       // tableData.push(row);
			 table_Category.appendRow(row)
        }
 		//table_Category.setData(tableData);
 		//Lhs.ui.activityIndicator.hideModal();
 		*/
 		require('settings').LoadingData2TableView(table_Category,json,CreateTableViewRow,15);
 		search_string.blur();
	};
	
	win.addEventListener('open', function (e) {
		/*
    	 if (Ti.Network.online) {
			//Load data when online
			var jsonurl='http://www.e-media.vn/emedia.php?gid=0';
			var param='';
			//Lhs.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
			require('settings').GetJsonFunction_GET(jsonurl,param,LoadList,LoadList_error);
			//Lhs.ui.activityIndicator.hideModal();
		 }
		 else
		 {
		 	//error in connect with internet
		 	//require('settings').createAlertDialog(Globals.title,Globals.connecterror);
		 	var ee=[{isconnect:0}];
			LoadList(ee);
		 	//LoadList_error()
		 }
		*/
		var ee=[{isconnect:0}];
		LoadList(ee);
		
	});
	
 	return win;
       
    };
})();